#include "qsearchresult.h"

QSearchResult::QSearchResult(QBDDItem* item,QWidget *parent)
	: QWidget(parent),m_item(item)
{
	ui.setupUi(this);
	hideResume();
	connect(ui.deploy,SIGNAL(clicked()),this,SLOT(deployChanged()));
	connect(ui.modify,SIGNAL(clicked()),this,SLOT(validResultPart()));
	if(item)
	{
		ui.plp->setText(rawToFormated(m_item->get(BDD_PLP)));
		ui.description->setText(rawToFormated(m_item->get(BDD_DESC)));
		ui.manufacturer->setText(rawToFormated(m_item->get(BDD_FOURNISSEUR)));
		ui.ref->setText(rawToFormated(m_item->get(BDD_REF)));
		ui.cat1->setText(rawToFormated(m_item->get(BDD_CAT_1)));
		ui.cat2->setText(rawToFormated(m_item->get(BDD_CAT_2)));
		ui.cat3->setText(rawToFormated(m_item->get(BDD_CAT_3)));
		ui.cat4->setText(rawToFormated(m_item->get(BDD_CAT_4)));

		ui.supplier_1->clear();
		ui.supplier_2->clear();
		ui.ref_supplier_1->clear();
		ui.ref_supplier_2->clear();
	}
}

QSearchResult::~QSearchResult()
{

}

void QSearchResult::hideResume()
{
	ui.manufacturer->hide();
	ui.ref->hide();
	ui.cat1->hide();
	ui.cat2->hide();
	ui.cat3->hide();
	ui.cat4->hide();

	ui.supplier_1->hide();
	ui.supplier_2->hide();
	ui.ref_supplier_1->hide();
	ui.ref_supplier_2->hide();

	setFixedHeight(ui.plp->maximumHeight());

	m_hide = true;
}

void QSearchResult::showResume()
{
	ui.manufacturer->show();
	ui.ref->show();
	ui.cat1->show();
	ui.cat2->show();
	ui.cat3->show();
	ui.cat4->show();

	ui.supplier_1->show();
	ui.supplier_2->show();
	ui.ref_supplier_1->show();
	ui.ref_supplier_2->show();

	

	m_hide = false;
}

void QSearchResult::deployChanged()
{
	if(m_hide)
		showResume();
	else
		hideResume();
}


void QSearchResult::validResultPart()
{
	QStringList part;
	part << formatedToRaw(ui.plp->text());
	part << formatedToRaw(ui.description->text());
	part << formatedToRaw(ui.manufacturer->text());
	part << formatedToRaw(ui.ref->text());	
	part << QString();
	emit partSelected(part);
}

QString QSearchResult::rawToFormated(QString raw) const
{
	return raw.remove("\"");
}

QString QSearchResult::formatedToRaw(QString formated) const
{
	return "\"" + rawToFormated(formated) + "\"";
}